package com.zhukunri.sp03.controller;

import com.zhukunri.sp01.domain.User;
import com.zhukunri.sp01.service.UserService;
import com.zhukunri.sp01.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    //根据用户id获取用户
    @GetMapping("/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        //根据id获取user
        User user = userService.getUser(userId);
        //封装成Json数据
        JsonResult result = JsonResult.ok().data(user);

        return result;
    }


    //给用户添加积分
    //用户访问路径：http://localhost:8101/8/score?score=1000
    @GetMapping("/{userId}/score")
    public JsonResult<?> addScore(@PathVariable Integer userId,Integer score){
        //给用户增加积分
        userService.addScore(userId,score );
        //封装成json数据
        JsonResult result = JsonResult.ok().msg("增加用户积分成功");

        return result;
    }

}
